package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.Enterprise;
import com.jr.pojo.Users;
import com.jr.service.EnterpriseService;
import com.jr.service.SupplierWhiteService;
import com.jr.service.UsersService;
import com.jr.service.impl.IEnterpriseServiceImpl;
import com.jr.service.impl.ISupperWhiteServiceImpl;
import com.jr.service.impl.IUserServiceImpl;
import com.jr.util.PageHelper;
import com.jr.vo.WhiteEnter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;

@WebServlet("/whiteenters/*")
public class WhiteEnterController extends HttpServlet {
    UsersService us = new IUserServiceImpl();
    SupplierWhiteService sws = new ISupperWhiteServiceImpl();
    EnterpriseService eps = new IEnterpriseServiceImpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // super.service(req, resp); //只根据【请求方式】分发；但是现在需要根据【请求路径】分发
        String requestURI = request.getRequestURI();
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index);
        Class cla = this.getClass();
        Method method = null;
        try {
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);
            response.setContentType("application/json;charset=utf-8");
            request.setCharacterEncoding("utf-8");
            method.invoke(this, request, response);

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (
                InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    public void showAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("下面开始时分页涉及到的输出内容DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD");
        PageHelper ph = new PageHelper();
        String whiteenter = request.getParameter("whiteenter");
        String level = request.getParameter("level");
        System.out.println(whiteenter + "企业名称 条件查询");
        System.out.println(level + "企业评级 条件查询");

        WhiteEnter we = new WhiteEnter();
        we.setEntername(whiteenter);
        we.setSupplierlevel(level);
        System.out.println(we.getSupplierlevel() + "::" + we.getEntername());

        ph.setPageIndex(Integer.parseInt(request.getParameter("pageindex")));
        System.out.println(ph.getPageIndex() + "从前面传过来的pageindex");

        /*需要用到当前账户的企业名称*/
        String account = request.getParameter("account");
        Users users = us.queryByAccount(account);
        System.out.println(users + "获得的对象");
        System.out.println(users.getEntername() + "获得的对象的企业名称");//拿到了 科技有限公司
        // 先查询总数据量
        ph.setDateCount(sws.queryWhiteCount(we, users.getEntername()));
        System.out.println(ph + "ph分页对象sssssss");
        // 查询当前页数据
        ArrayList<WhiteEnter> whitelist = sws.queryWhite(we,ph,users.getEntername());

        ph.setListIndex(whitelist);
       // System.out.println(ph.getListIndex()+"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
       /*拿信息传回 jsp请求函数*/
        response.getWriter().println(new Gson().toJson(ph));
    }

    /*jsp发请求 获得满足要求的企业名称*/
    public void queryEnterName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*直接获得sql语句的查询结果没有条件*/
        //eps.queryEnterprise()
        /*这块我给的企业名称是当前管理人员所在的企业名称*/
        String account = request.getParameter("account");
        Users users = us.queryByAccount(account);
        /*  System.out.println(users+"获得的对象");*/
        /*  System.out.println(users.getEntername()+"获得的对象的企业名称");//拿到了 科技有限公司*/
        /*根据当前登录的用户 我可以获得可以选择的企业名称*/
        //ArrayList list =  sws.queryWhiteEnter(users.getEntername());
        Map<Integer, String> integerStringMap = sws.queryWhiteEnter(users.getEntername());
        //ArrayList<String> valueList = new ArrayList<String>(integerStringMap.values());
        // System.out.println(valueList+"可选企业名字集合");
        response.getWriter().println(new Gson().toJson(integerStringMap));//直接传map集合
        //System.out.println("通过当前用户获得可选择企业名字"+list.get(0));
    }

    public void queryUnCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String entername = request.getParameter("selectedText");
        Enterprise enter = eps.queryENterByName(entername);
        /*获得对象 -- 获得对象的 统一社会代码*/
        String code = enter.getUniformcode();
        response.getWriter().println(new Gson().toJson(code));
    }

    public void addWhiteEnter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String account = request.getParameter("account");
        String addentername = request.getParameter("selectedText");//选择加入比啊名单的企业名称
        String level = request.getParameter("selectLevel");//选择的评级
        Users users = us.queryByAccount(account);
        String nowentername = users.getEntername();//当前对象的企业名称
        boolean b = sws.addWhiteEnter(nowentername, addentername, level/* System.out.println("成功添加白名单吗？");*/, LocalDate.now());
        response.getWriter().println(new Gson().toJson(b));
    }

    public void getcode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
       String whitename = request.getParameter("whitename");

        System.out.println(whitename+"删除企业名字");
       Enterprise whiteenter = eps.queryENterByName(whitename);
       String code = whiteenter.getUniformcode();
        /*拿到供应商的统一代码*/
        System.out.println("CCCCCCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDDDDDDD");
        response.getWriter().println(new Gson().toJson(code));
    }


    public void addBlackEnter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /* var enterpriseId = $("#blacklistEnterpriseId").val();
                var enterpriseName = $("#blacklistEnterpriseName").text();
                var account = $('em[name="loginaccount"]').text();*/
        /*添加进黑名单 获得 该企业的统一社会代码*/
        /*在把白名单中的某个企业添加进黑名单时 先调用添加方法随即调用删除方法
        * 1.需要再dao层有两个方法 添加黑名单  删除白名单 */
        //需要 当前用户所在企业的id 需要 选择的企业的id
        //我已经获得了当前企业的id 我可以根据 企业id 以及企业名称 去白名单中查询供应商id
        //1.获得当前企业的id 根据账号获得用户所在企业 根据企业名称获得企业id
        /*获得删除企业的统一代码*/

            String whitename = request.getParameter("whitename");
            System.out.println(whitename+"删除企业名字");
            Enterprise whiteenter = eps.queryENterByName(whitename);
            String code = whiteenter.getUniformcode();
            /*拿到供应商的统一代码*/
          //  response.getWriter().println(new Gson().toJson(code));
        /*获得当前对象所在企业名称*/
            String account = request.getParameter("account");
            Users users1 = us.queryByAccount(account);
            users1.getEntername();//当前管理员所在企业
        /*获得删除企业名称*/
            //String whitename = request.getParameter("whitename");
        /*删除白名单 +  添加黑名单*/
            /*获得两个企业的id*/
            Enterprise myenter = eps.queryENterByName(users1.getEntername());
            Enterprise delenter = eps.queryENterByName(whitename);
            myenter.getId();//当前企业id
            delenter.getId();//删除企业id
            boolean in = sws.queryWhiteInBlack(myenter.getId(),  delenter.getId());
            if (in==false){
                /*添加黑名单*/
                boolean add = sws.addBlackEnter(myenter.getId(),delenter.getId()," ", LocalDate.now());
                System.out.println(add+"添加进黑名单");
                /*删除白名单*/
                boolean del = sws.dropWhiteENter(myenter.getId(), delenter.getId());
                System.out.println(del+"在白名单中删除");
                response.getWriter().println(new Gson().toJson(add));
            }


        /*添加进黑名单 然后白名单相对应减少一条相关数据*/
        /*怎么达到点击 删除连接 就可以 找到对应的公司删除该公司的某企业的供应信息
         * 获得 该供应商在企业表中的企业id */
    }


}